package com.ywq.wechatordersys.controller;

import com.ywq.wechatordersys.VO.CategoryVO;
import com.ywq.wechatordersys.VO.ProductInfoVO;
import com.ywq.wechatordersys.data.ProductCategory;
import com.ywq.wechatordersys.data.ProductInfo;
import com.ywq.wechatordersys.VO.Result;
import com.ywq.wechatordersys.service.impl.CategoryServiceImpl;
import com.ywq.wechatordersys.service.impl.ProductInfoServiceImpl;
import com.ywq.wechatordersys.utils.ResultUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping(value = "/buyer/product")
public class BuyerProductController {

    @Autowired
    private ProductInfoServiceImpl productInfoService;
    @Autowired
    private CategoryServiceImpl categoryService;

    /**
     * 查询单个商品
     */
    @GetMapping(value = "/get")
    public Result<ProductInfoVO> one(@RequestParam(value = "id", required = true) String id) {

        if (id == null || id.isEmpty()) {
            return ResultUtils.fail(100, "product id is missing");
        }
        ProductInfo product = productInfoService.getProduct(id);
        ProductInfoVO productInfoVO = new ProductInfoVO();
        BeanUtils.copyProperties(product,productInfoVO);
        return ResultUtils.success(productInfoVO);

    }

    /**
     * 查询上架的商品
     */
    @GetMapping(value = "/list")
    public Result<List<CategoryVO>> list() {
//1.查询所有上架的商品
        List<ProductInfo> productInfos = productInfoService.getAllUpProducts();


        //2.查询类目

        List<Integer> collect = productInfos.stream().map(
                it -> it.getCategoryType()
        ).collect(
                Collectors.toList()

        );

        List<ProductCategory> categoryTypes = categoryService.findByCategoryTypes(collect);

        ArrayList<CategoryVO> categoryVOS = new ArrayList<>();
        //首先遍历类目

        for (ProductCategory categoryType : categoryTypes) {

            CategoryVO categoryVO = new CategoryVO();
            BeanUtils.copyProperties(categoryType, categoryVO);
            ArrayList<ProductInfoVO> productInfoVOS = new ArrayList<>();
            for (ProductInfo productInfo : productInfos) {

                if (productInfo.getCategoryType() == categoryType.getCategoryType()) {
                    ProductInfoVO productInfoVO = new ProductInfoVO();

                    BeanUtils.copyProperties(productInfo, productInfoVO);
                    productInfoVOS.add(productInfoVO);
                }
            }

            categoryVO.setProducts(productInfoVOS);
            categoryVOS.add(categoryVO);
        }
        return ResultUtils.success(categoryVOS);

    }


}
